import json

"""
py json 
1.提供了 dumps 将对象转换成 json
2.dump方法 可以将json内容直接写入到文件中
也只有基本数据类型才能被转化成json
int float str list  dict tuple
"""
entity = [
    {
        "name": 'summit',
        "age": '15'
    },
    {
        'name': 'jack',
        "age": '18'
    }
]


def obj2_json():
    dumps = json.dumps(entity)

    # print(dumps)
    return dumps


def json2_file():
    with open("test_file/test.json", "w", encoding="utf-8") as fp:
        json.dump(entity, fp, ensure_ascii=False)


# json 转换成py对象
def json_loads_obj():
    json_obj = obj2_json()
    objs = json.loads(json_obj)
    for obj in objs:
        print("对象:", obj)


def file2_obj():
    with open("test_file/test.json", "r", encoding="utf-8") as fp:
        entitys = json.load(fp)
        for entity in entitys:
            print(entity)


if __name__ == '__main__':
    # json_loads_obj()
    file2_obj()
